<template>
  <div class="comment">
    <p class="title">
      评论<span>({{ commentlen }})</span>
    </p>
    <div class="send">
      <div class="avatar">
        <img src="../../../assets/img/avatar.jpg" alt="" />
      </div>
      <van-field
        v-model.trim="content"
        placeholder="说点什么吧"
        ref="addInput"
      />
      <button @click.prevent="addComment">发表评论</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      content: "",
    };
  },
  methods: {
    addComment() {
      if (!localStorage.getItem("token") && !localStorage.getItem("id")) {
        this.$toast("请先登录");
        return this.$router.push({
          path: "/login", // 将跳转的路由path作为参数，登录成功后跳转到该路由
          query: { redirect: this.$route.fullPath }
        })
      }

      // 将事件传递给父组件
      this.$emit('addComment', this.content)
      this.content = ''
    },
    addInput(){
      this.$refs.addInput.focus()
    }
  },
  props: {
    commentlen: {
      type: Number,
      default: 0
    }
  }
};
</script>
<style lang="less" scoped>
.comment {
  padding: 0 10px;
  margin-top: 10px;
  .title {
    span {
      color: #999;
    }
  }
  .send {
    margin-top: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 40px;
    overflow: hidden;
    .avatar {
      flex: 1;
      img {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        padding: 7px 0;
      }
    }
    .van-cell {
      flex: 6;
      padding: 5px 0 0;
    }
    button {
      border: none;
      outline: none;
      background-color: #02a774;
      color: #fff;
      // height: 30px;
      padding: 0 5px;
      margin: 7px;
      line-height: 30px;
      border-radius: 10px;
      font-size: 13px;
      width: 20%;
    }
  }
}
</style>
